package cn.tedu.charging.user.dao.repository;

import cn.tedu.charging.common.pojo.po.ChargingUserVehicleBindPO;
import cn.tedu.charging.user.dao.mapper.ChargingUserVehicleBindMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class UserVehicleRepository {
    @Autowired
    private ChargingUserVehicleBindMapper chargingUserVehicleBindMapper;

    public Integer getUserBindedVehicleId(Integer userId,Integer state){
        QueryWrapper<ChargingUserVehicleBindPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",state);
        ChargingUserVehicleBindPO po = chargingUserVehicleBindMapper.selectOne(queryWrapper);
        if (po!=null){
            return po.getVehicleId();
        }else {
            return null;
        }
    }

    public void save(ChargingUserVehicleBindPO userVehicle) {
        chargingUserVehicleBindMapper.insert(userVehicle);
    }

    public boolean updateUserVehicleState(Integer userId) {
        UpdateWrapper<ChargingUserVehicleBindPO> updateWrapper = new UpdateWrapper<>();
        ChargingUserVehicleBindPO poParam=new ChargingUserVehicleBindPO();
        poParam.setState(0);
        poParam.setUnbindTime(new Date());
        updateWrapper.eq("user_id",userId);
        updateWrapper.eq("state",1);
        int rows = chargingUserVehicleBindMapper.update(poParam, updateWrapper);
        return rows>0;
    }
}
